//
//  AppDelegate.m
//  doubanFun
//
//  Created by bing on 16/3/21.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "AppDelegate.h"
#import "DFChannelDAO.h"
#import "DFSongPlayer.h"
#import "MainViewController.h"
#import "LoginViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    [DFChannelDAO sharedDAO];
    [self makeMainView];
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // 挂起
    [self saveUserLastActive];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // 已经进入后台
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // 将要进入后台
    [[DFSongPlayer sharedplayer] backgroudTask];
    [self saveUserLastActive];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // 恢复
    [self saveUserLastActive];
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // 程序终止
    [self saveUserLastActive];
}

- (BOOL)canBecomeFirstResponder {
    return YES;
}

// 响应控制台
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
    if (event.type == UIEventTypeRemoteControl) {
        switch (event.subtype) {
            case UIEventSubtypeRemoteControlPlay:{
                NSLog(@"暂停");
            }
                break;
            case UIEventSubtypeRemoteControlPause:{
                NSLog(@"继续");
            }
                break;
            case UIEventSubtypeRemoteControlNextTrack:{
                NSLog(@"下一曲");
            }
                break;
            case UIEventSubtypeRemoteControlPreviousTrack:{
                NSLog(@"上一曲");
            }
                break;
                
            default:
                break;
        }
    }
}


#pragma mark - other

- (void)makeMainView {
    MainViewController *mainVC = [[MainViewController alloc] init];
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.rootViewController = mainVC;
    [self.window makeKeyAndVisible];
    
    if (self.user) {
        return;
    }
    LoginViewController *loginVC = [[LoginViewController alloc] init];
    [mainVC presentViewController:loginVC animated:YES completion:nil];
}

- (DFConfig *)config {
    if (!_config) {
        _config = [[DFConfig alloc] init];
        NSString *channelID = [[NSUserDefaults standardUserDefaults] stringForKey:@"lastChannelID"];
        if ((channelID && channelID.length < 1)||!channelID) {
            channelID = @"0";
        }
        NSString *songID = [[NSUserDefaults standardUserDefaults] stringForKey:@"lastSongID"];
        
        _config.currentChannelID = channelID;
        _config.currentSongID    = songID;
    }
    return _config;
}

- (DFUser *)user {
    if (!_user) {
        NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:@"user"];
        _user = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    return _user;
}

// 标记用户最后的操作
- (void)saveUserLastActive {
    NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
    if (self.config.currentChannelID.length > 0) {
        [userDefault setObject:self.config.currentChannelID forKey:@"lastChannelID"];
    }
    if (self.config.currentSongID.length > 0) {
        [userDefault setObject:self.config.currentSongID forKey:@"lastSongID"];
    }
}
@end
